﻿#include"StashManageModelObject.h"
#include"XQMail.h"
StashManageModelObject::StashManageModelObject()
{
}

StashManageModelObject::StashManageModelObject(XQMySql* mysql, XQMail* mail,XQSettingsInfo* settings, XQThreadQueuePool* pool)
{
	m_mysql = mysql;
	m_mail = mail;
	m_settings = settings;
	m_pool = pool;
}

StashManageModelObject::~StashManageModelObject()
{
	if (m_pool)
	{
		m_pool->requestInterruptionTaskAll();
		//m_pool->wait_event();
	}
}

XQUserData StashManageModelObject::userData()
{
	if (m_userInfo == nullptr)
		return XQUserData();
	return m_userInfo->userData();
}

UserState StashManageModelObject::userState()
{
	if(m_userInfo ==nullptr)
		return UserState::Logout;
	return m_userInfo->userState();
}

XQMail* StashManageModelObject::mail() const
{
	return m_mail;
}

XQMySql* StashManageModelObject::mysql()const
{
	return m_mysql;
}

XQSettingsInfo* StashManageModelObject::settings() const
{
	return m_settings;
}

XQThreadQueuePool* StashManageModelObject::pool() const
{
	return m_pool;
}

XQUserInfo* StashManageModelObject::userInfo() const
{
	return m_userInfo;
}

bool StashManageModelObject::connectionMySql(XQDatabaseConnectionModel data)
{
	if(m_mysql==nullptr)
		return false;
	return m_mysql->connection("StashManage",data.hostName(),data.port(),data.userName(),data.password(),data.databaseName());
}

XQThreadTask* StashManageModelObject::addMySqlThreadTask(XQThreadTask* task)
{
	if (m_pool == nullptr)
		return nullptr;
	return m_pool->addTask("MySql",task);
}
XQThreadTask* StashManageModelObject::addMySqlThreadTask(std::function<void(XQThreadTask*)>&& task)
{
	if (m_pool == nullptr)
		return nullptr;
	return m_pool->addTask(QString("MySql"), (std::function<void(XQThreadTask*)>&&) task);
}
XQThreadTask* StashManageModelObject::addMySqlThreadTask(std::function<void()>&& task)
{
	if (m_pool == nullptr)
		return nullptr;
	return m_pool->addTask(QString("MySql"),(std::function<void()>&&) task);
}

bool StashManageModelObject::sendVerifyMail(const QString& email, const QString& mail_title, qint64 verify)
{
	auto mail = this->mail();
	if (mail == nullptr)
		return false;

	mail->setReceiver("Manage", email);
	if (mail->connent_ssl())
	{
		mail->SendMail(mail_title, "验证码:" + QString::number(verify));
		return true;
	}
	return false;
}


void StashManageModelObject::setUserData(const XQUserData& userData)
{
	if (m_userInfo != nullptr)
		m_userInfo->setUserData(userData);
		//m_userInfo = new XQUserInfo();
	
		//m_userInfo = QSharedPointer<XQUserData>::create();
	//*m_userInfo = userData;
}

void StashManageModelObject::setUserState(UserState state)
{
	if(m_userInfo!=nullptr)
		m_userInfo->setUserState(state);
}

void StashManageModelObject::setMySql(XQMySql* mysql)
{
	m_mysql = mysql;
}

void StashManageModelObject::setMail(XQMail* mail)
{
	m_mail = mail;
}

void StashManageModelObject::setSettings(XQSettingsInfo* settings)
{
	m_settings=settings;
}

void StashManageModelObject::setUserInfo(XQUserInfo* info)
{
	m_userInfo = info;
}
